﻿//using System;
//using System.Collections.Generic;
//using System.Text;
//using DCRF.Common.Core;
//using DCRF.Common.Primitive;
//using DCRF.Common.Interface;

//namespace DCRF.BaseBlocks
//{
//    public class BlockWebManager : BlockBase
//    {
//        private Dictionary<Guid, DateTime> lastOperations = new Dictionary<Guid, DateTime>();
//        /// <summary>
//        /// How many Blocks can be added to this BlockWeb?
//        /// </summary>
//        int Capacity
//        {
//            get
//            {
//                return (int)Properties["Capacity"];
//            }
//            set
//            {
//                Properties["Capacity"] = value;
//            }
//        }

//        /// <summary>
//        /// If a block is not called for this period of time, delete from web
//        /// </summary>
//        TimeSpan BlockLifetimeLimit
//        {
//            get
//            {
//                return (TimeSpan)Properties["LifetimeLimit"];
//            }
//            set
//            {
//                Properties["LifetimeLimit"] = value;
//            }
//        }

//        public override BlockInfo BlockInfo
//        {
//            get
//            {
//                BlockInfo result = new BlockInfo();
//                result.BlockID = new CID("BlockWebManager", new BlockVersion(1, 0, 0, 0), "DCRF");
//                return result;
//            }
//        }

//        public override void InitProperties()
//        {
//            Properties.Add("Capacity", typeof(int),false,0);
//            Properties.Add("LifetimeLimit", typeof(TimeSpan), false, TimeSpan.MinValue);
//        }

//        void BlockWeb_AddingBlock(IBlockWeb sender, CID subject, ref bool cancelOperation)
//        {
//            //check capacity
//            if (Capacity != 0 && sender.Count >= Capacity)
//            {
//                //cancel Block addition
//                cancelOperation = true;
//            }
//        }

//        void BlockWeb_BlockAdded(IBlockWeb sender, Guid guid)
//        {
//            lastOperations[guid] = DateTime.Now;
//        }

//        void BlockWeb_PreBlockCall(IBlockWeb sender, Guid guid, string serviceName, object[] args, ref 
//            object response, ref bool cancelCall)
//        {
//            lastOperations[guid] = DateTime.Now;

//            if (BlockLifetimeLimit != TimeSpan.MinValue)
//            {
//                scanBlockWebBlocksLifetime(sender);
//            }
//        }

//        private void scanBlockWebBlocksLifetime(IBlockWeb BlockWeb)
//        {
//            List<Guid> toBeDeleted = new List<Guid>();

//            foreach (Guid cmp in BlockWeb.BlockHandles)
//            {
//                TimeSpan diff = DateTime.Now.Subtract(lastOperations[cmp]);

//                if (diff > BlockLifetimeLimit)
//                {
//                    toBeDeleted.Add(cmp);
//                }
//            }

//            foreach (Guid cmp in toBeDeleted)
//            {
//                BlockWeb.DeleteBlock(cmp);
//                lastOperations.Remove(cmp);
//            }
//        }

//        public override void StartupConstructor(List<object> args)
//        {
//            BlockWeb.AddingBlock += new BlockWebActionPreEventDelegate(BlockWeb_AddingBlock);
//            //TODO: fix BlockWeb.PreBlockCall += new PreBlockCallEventDelegate(BlockWeb_PreBlockCall);
//            BlockWeb.BlockAdded += new BlockWebEventDelegate(BlockWeb_BlockAdded);
//        }

        
//    }
//}
